#define _CRT_SECURE_NO_WARNINGS 1

//1
void* my_memmove(void* dst, const void* src, size_t count)
{
    void* ret = dst;
    if (dst <= src || (char*)dst >= ((char*)src + count)) {
        while (count--) {
            *(char*)dst = *(char*)src;
            dst = (char*)dst + 1;
            src = (char*)src + 1;
        }
    }
    else {
        dst = (char*)dst + count - 1;
        src = (char*)src + count - 1;
        while (count--) {
            *(char*)dst = *(char*)src;
            dst = (char*)dst - 1;
            src = (char*)src - 1;
        }
    }
    return(ret);
}


int main() {
    char arr[10] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J' };


    for (int i = 0; i < 10; i++) {
        printf("%c ", arr[i]);
    }
    printf("\n");

    my_memmove(&arr[3], &arr[1], 5);


    for (int i = 0; i < 10; i++) {
        printf("%c ", arr[i]);
    }
    printf("\n");

    return 0;
}

//2
void* memcpy(void* dst, const void* src, size_t count)
{
    void* ret = dst;
    assert(dst);
    assert(src);
    /*
    * copy from lower addresses to higher addresses
    */
    while (count--) {
        *(char*)dst = *(char*)src;
        dst = (char*)dst + 1;
        src = (char*)src + 1;
    }
    return(ret);
}